<h2 style='text-align:center'>域名地址管理</h2>

<h5 style='color:red'>注：当upyun端域名列表缓存丢失时，刷新本页会重新生成!</h5>

<button id='MainDomian_Add'>添加服务器域名</button>　
<button id='MainDomian_updateUpyun'>更新upyun</button>
<div id='MainDomian_AddUserArea' style='display:none'>
	<div><input id='MainDomian_AddUrl' placeholder='域名http://...' size=50 /></div>
	<div><input id='MainDomian_AddInfo' placeholder='备注' size=50 /></div>
	<div>
		<button id='MainDomian_AddBtn'>添加</button>
		<button id='MainDomian_AddCancel'>取消</button>
	</div>
</div>


<div style='margin:20px auto' id='MainDomian_ListArea'>
	<div style='border-bottom:1px solid #999;margin:5px auto;display:flex'>
		<span style='margin:5px 10px;flex:1;word-break:break-all '>a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1</span>
		<span style='flex:1;margin:5px 10px;word-break:break-all'>sdfsdfsfsdfsdfsfsdfsdfsfsdfsdfsfsdfsdfssdfsdfsfsdfsdfsfsdfsdfsfsdfsdfsfsdfsdfsf</span>
		<span style='margin:5px 10px '><button>删除</button></span>
	</div>
</div>
<div style='margin:5px 10px;' id='MainDomain_SelectAllArea'>
	<label><input type="checkbox" id='MainDomain_SelectAll' /> 全选/全不选</label>　
	<button id='MainDomain_SelectAllMulti'>删除</button>
</div>



<script>


	//获取服务器域名列表
	_getDomainListFull(function(domainListFull){
		C(domainListFull);

		var tmp = _checkJsonFormat(domainListFull);
		var domainListFull = tmp.data;
		if(!/^\[.*\]$/.test(domainListFull)){
			domainListFull = [];
		}
		_parseDomain(domainListFull);
	});


	//添加服务器域名列表
	$('#MainDomian_Add').click(function(){
		$('#MainDomian_AddUserArea').show();
	});

	//隐藏添加区域
	$('#MainDomian_AddCancel').click(function(){
		$('#MainDomian_AddUserArea').hide();
	});


	//更新upyun
	$('#MainDomian_updateUpyun').click(function(){
		Notice('处理中......');
		AjaxAdmin({type:'updateUpyunDomainList'},function(res){
			Notice();
			var tmp = _checkJsonFormat(res);
			if(tmp.stat == 'ok'){
				A('操作成功！');
			}else{
				A('操作异常！');
			}
		});
	});

	//域名添加
	$('#MainDomian_AddBtn').click(function(){
		var domain = encodeURIComponent($('#MainDomian_AddUrl').val().trim());
		var info = ChangeChar($('#MainDomian_AddInfo').val().trim());
		if(!domain || !info){
			return;
		}
		var data = {
			type:'domainAdd',
			domain:domain,
			info:info
		};
		AjaxAdmin(data,function(res){
			var tmp = _checkJsonFormat(res);
			if(tmp.stat == 'ok'){
				A('操作成功！');
				$('#MainDomian_AddUrl').val('');
				$('#MainDomian_AddInfo').val('');
				$('#MainDomian_AddUserArea').hide();
				location.reload();
			}else if(tmp.stat == 'err2'){
				A('域名已存在！');
			}else{
				A('操作失败！');
			}
	
		});
	});



	function _parseDomain(list){
		var str = '';
		for(var i=0;i<list.length;i++){
			str += "<div style='border-bottom:1px solid #999;margin:5px auto;display:flex;word-break:break-all'>";
			str += "	<label><span style='margin:5px 10px;'><input type='checkbox' value='"+list[i].id+"' /></span>";
			var domain2 = decodeURIComponent(list[i].domain);
			str += "	<span style='margin:5px 10px;flex:1;word-break:break-all '>"+domain2+"</span></label>";
			str += "	<span style='flex:1;margin:5px 10px;word-break:break-all;color:#777'>"+list[i].info+"</span>";
			str += "	<span style='margin:5px 10px'><button onclick=MainDomian_Del("+list[i].id+")>删除</button></span>";
			str += "</div>";
		}
		$('#MainDomian_ListArea').html(str);

		//全选/全不选
		$('#MainDomain_SelectAll').click(function(){
			if($(this).is(':checked')){
				$('#MainDomian_ListArea').find('input[type=checkbox]').each(function(i,o){
					o.checked = true;
				});
			}else{
				$('#MainDomian_ListArea').find('input[type=checkbox]').each(function(i,o){
					o.checked = false;
				});
			}
		});




		//多选删除
		$('#MainDomain_SelectAllMulti').click(function(){
			if(!_confirm()){
				exit3
			}
			var id_arr = [];
			$('#MainDomian_ListArea').find('input[type=checkbox]').each(function(i,o){
				if(o.checked == true){
					id_arr.push($(o).val());
				}
			});
			var id_str = id_arr.join(',');
			if(!id_str){
				return;
			}
			AjaxAdmin({type:'domainDelMulti',ids:id_str},function(res){
				var tmp = _checkJsonFormat(res);
				if(tmp.stat == 'ok'){
					A('操作成功！');
					location.reload();
				}else{
					A('操作失败！');
				}
			});

		});
	}


	//删除域名
	function MainDomian_Del(id){
		if(!confirm('确认删除?')){
			exit2
		}
		var data = {
			type:'domainDel',
			id:id
		};
		AjaxAdmin(data,function(res){
			var tmp = _checkJsonFormat(res);
			if(tmp.stat == 'ok'){
				A('操作成功！');
				location.reload();
			}else{
				A('操作失败!');
			}
		});
	}


</script>